#!/bin/csh -f

setenv y4 `date +"%Y"`

if ( $#argv > 1 ) then
   echo 'Usage :' $0 or $0 ccyy
   exit 1
else if ( $#argv == 1 ) then
   setenv y4 $1
endif

# go to system dir and set environment variables
#####################
cd /home/dataop/scripts-data/
source ./settings

###########################################

cd "$datadir/data-status/$y4/"
foreach file ( `ls -1  data-status*` )
	#TODO check status.
	#echo $file
	set runtime=`echo $file | grep -o '[0-9]\{10\}'`
	#echo $runtime
	foreach download_job ( 'gfs' 'gts')
		set check = `$downscriptdir/read-status.csh $runtime ${download_job}`
		#echo $check
		if ( "$check" == "9" ) then
			set t1="`stat $file | grep 'Modify' | grep -o '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\ [0-9][0-9]:[0-9][0-9]:[0-9][0-9]'`"
			set t1_s=`date -d"$t1" +"%s"`
#			echo $t1_s
			set tnow_s=`date +"%s"`
#			echo $tnow_s
			@ tdiff_s = $tnow_s - $t1_s
#			echo $tdiff_s
			echo checking $file, Last Modified: $t1, Now: `date +"%Y-%m-%d %H:%M:%S"` ,time diff: $tdiff_s seconds
			if ( $tdiff_s > $max_time_out_seconds ) then
				$downscriptdir/modify-status.csh $runtime ${download_job} 0
				echo Timeout. Modified.
			endif
		endif
	end
end

